
<?php

/*EVENT IS THE ACTION USER PERFOMING
 *
 * 100 : Posting a comment
 * 150 : Navgating on results
 *
*/

include 'logic/Connection.php';

$event = $_GET['event'];
$dbLink = new Connection(); // CREATING A NEW CONNECTION OBJECT

if($event == 100) {
    $user = $_GET['user'];
    $email = $_GET['email'];
    $post = $_GET['comment'];
    echo '<div class="author"><img class="avatar" src="images/demo/avatar.gif" width="32" height="32" alt="" />
                                <span class="name"><a href="#">'.$user.'</a></span>
                                <span class="wrote">wrote:</span></div>
                                <div class="submitdate"><a href="#">'.date('l jS \of F Y h:i:s A').'</a></div>
                                <p>'.$post.'</p>';
}

if ($event == 150) {
    $limit = 4;
    $lastpage = 0;
    if(isset ($_GET['lastIndex']))
        $lastpage = $_GET['$lastpage'];        //LAST INDEX OF PREVIOUS RESULTSET

    if(isset ($_GET['direction']))
        $direction = $_GET['direction'];        //FORWARD OR BACKWARD


    if(isset ($_GET['fltBrand']))
        $sqlF1 = $_GET['fltBrand'];             //LOADING THE BRAND
    else
        $sqlF1 = "%";

    if(isset ($_GET['fltPrice']))
        $sqlF2 = $_GET['fltPrice'];             //LOADING THE PRICE
    else
        $sqlF2 = "%";

    if(isset ($_GET['fltFeature']))
        $sqlF3 = $_GET['fltFeature'];           //LOADING THE FEATURE
    else
        $sqlF3 = "%";

    if(isset ($_GET['currPage']))
        $currPage = 10;
    else {
        $currPage=150;
    }

    //GETTING THE TOTAL NUMBER OF RECORDS
//    $defaultQuery = "SELECT * FROM mp_tb_product ORDER BY MP_PRD_ID asc ";
//    $res = mysql_query($defaultQuery) or die (mysql_error());
//    $numRec = mysql_num_rows($res);

//    if($_GET['direction']=="none") {
//        $defaultQuery .= " LIMIT 0, " . $limit;
//        $currPage = 0;
//    }
//    else {

    if($currPage>$lastpage) {
        $currPage = 500;
    }

    if($currPage<1) {
        $currPage = 200;
    }

    $lastpage;// = ceil($numRec/$limit);
    $defaultQuery="";
    if($direction=="fw") {
        //if($currPage<$lastpage) {
            $defaultQuery = "SELECT * FROM mp_tb_product WHERE MP_PRD_BRAND LIKE '$sqlF1' AND MP_PRD_FEATURE LIKE '$sqlF2'
            AND MP_PRD_PRICE BETWEEN 10" . " AND 100000 ". " ORDER BY MP_PRD_ID" ;
            $lastpage = mysql_num_rows(mysql_query($defaultQuery)) or die (mysql_error());
            $defaultQuery .= " LIMIT " . (($currPage-1)*$limit) . ", " .$limit;
        //}
    }
    if($direction=="bk") {
        ///if($currPage>2) {
            $defaultQuery = "SELECT * FROM mp_tb_product WHERE MP_PRD_BRAND LIKE '$sqlF1' AND MP_PRD_FEATURE LIKE '$sqlF2'
            AND MP_PRD_PRICE BETWEEN 10" . " AND 100000" . " ORDER BY MP_PRD_ID";
            $lastpage = mysql_num_rows(mysql_query($defaultQuery)) or die (mysql_error());
            $defaultQuery .= " LIMIT " .(($currPage-2)*$limit) . ", " .$limit;
       // }
    }
    if($direction=="none") {
        $defaultQuery = "SELECT * FROM mp_tb_product WHERE MP_PRD_BRAND LIKE '%" . "' AND MP_PRD_FEATURE LIKE '%".
                "' AND MP_PRD_PRICE BETWEEN 10" . " AND 100000 ". " ORDER BY MP_PRD_ID";
        $lastpage = mysql_num_rows(mysql_query($defaultQuery)) or die (mysql_error());
        $defaultQuery .=" LIMIT 0, " .$limit;
    }
//    }

    $res = mysql_query($defaultQuery) or die ("Fuck");
    $numRec = mysql_num_rows($res);

    //$currPage = ceil($numRec/$lastIndex);
    if($currPage!=1)
        $tempCurr = $currPage + 1;
    else
        $tempCurr = $currPage;

    $output = "{ \"data\" : [";
    while($row = mysql_fetch_assoc($res)) {
        $output .="{\"id\" : \"$row[MP_PRD_ID]\", \"name\" : \"$row[MP_PRD_NAME]\", \"desc\" : \"$row[MP_PRD_DESCRIPTION]\",
                \"price\" : \"$row[MP_PRD_PRICE]\", \"url\" : \"$row[MP_PRD_IMAGE]\", \"cat\" : \"$row[MP_PRD_CATEGORY]\",
                \"brand\" : \"$row[MP_PRD_BRAND]\", \"model\" : \"$row[MP_PRD_MODEL]\", \"feature\" : \"$row[MP_PRD_FEATURE]\",
                \"dim\" : \"$row[MP_PRD_DIMS]\", \"unitL\" : \"$row[MP_PRD_MEASURES]\", \"weight\" : \"$row[MP_PRD_WEIGHT]\",
                \"unitW\" : \"$row[MP_PRD_UNIT]\", \"lastPage\" : \"$lastpage\", \"cPage\" : \"$tempCurr\", \"miss\" : \"$lastpage\" },";
    }
    $output  =  substr($output, 0, strlen($output)-1);
    $output.="]}";
    //$row[MP_PRD_DESCRIPTION]
    echo $output;
}

if($event==200) {
    $type = $_GET['type'];
    $url="";
    if($type=="all") {
        $url = "http://".$_SERVER["SERVER_NAME"]."/PhoneMall/browse.php";
    }
    else {
        $usrl = "http://".$_SERVER["SERVER_NAME"]."/PhoneMall/browse.php?".$type;
    }
    header("Location:".$url);
}


/*
 *
 * $rows_per_page = 4;
$lastpage      = ceil($numrows/$rows_per_page);

$pageno = (int)$pageno;
if ($pageno > $lastpage) {
   $pageno = $lastpage;
}
if ($pageno < 1) {
   $pageno = 1;
}

$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;

//________________________________________________________________________

if ($pageno == 1) {
   echo " FIRST PREV ";
} else {
   echo " <a href='{$_SERVER['PHP_SELF']}?pageno=1&ac=$product'>FIRST</a> ";
   $prevpage = $pageno-1;
   echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage&ac=$product'>PREV</a> ";

   }
   echo " ( Page $pageno of $lastpage ) ";
if ($pageno == $lastpage) {
   echo " NEXT LAST ";
} else {
   $nextpage = $pageno+1;
   echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage&ac=$product'>NEXT</a> ";
   echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$lastpage&ac=$product'>LAST</a> ";
}
*/

?>


